package com.syedu.day15.homework;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * 三条线程分别打印1-52的数字,A-Z的大写字母,a-z的小写字母
 * 最终要求使用线程通信方式完成如下打印顺序:
 * 12Aa34Bb56Cc...5152Zz
 */
public class Exercise5 {
    static Thread threadNumber;
    static Thread threadUpperLetter;
    static Thread threadLowerLetter;

    public static void main(String[] args) throws InterruptedException {
        threadNumber = new Thread(() -> {
            for (int i = 1; i <= 26; i++) {
                System.out.print(i * 2 - 1);
                System.out.print(i * 2);
                LockSupport.unpark(threadUpperLetter);
                LockSupport.park();
            }
        });
        threadUpperLetter = new Thread(() -> {
            List<Character> upperLetters = getUpperLetters();
            for (int i = 0; i < upperLetters.size(); i++) {
                LockSupport.park();
                System.out.print(upperLetters.get(i));
                LockSupport.unpark(threadLowerLetter);
            }
        });

        threadLowerLetter = new Thread(() -> {
            List<Character> lowerLetters = getLowerLetters();
            for (int i = 0; i < lowerLetters.size(); i++) {
                LockSupport.park();
                System.out.print(lowerLetters.get(i));
                LockSupport.unpark(threadNumber);
            }
        });
        threadNumber.start();
        threadUpperLetter.start();
        threadLowerLetter.start();
    }

    public static List<Character> getLowerLetters() {
        List<Character> list = new ArrayList<>();
        for (char c = 'a'; c <= 'z'; c++) {
            list.add(c);
        }
        return list;
    }

    public static List<Character> getUpperLetters() {
        List<Character> list = new ArrayList<>();
        for (char c = 'A'; c <= 'Z'; c++) {
            list.add(c);
        }
        return list;
    }
}